package actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import forms.DegreeForm;
import forms.FileUploadForm;

public class FileUploadAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		FileUploadForm myForm = (FileUploadForm) form;

		// Process the FormFile
		FormFile myFile = myForm.getMyFile();
		String contentType = myFile.getContentType();
		String filename = myFile.getFileName();
		
		int filesize = myFile.getFileSize();
		byte[] fileData = myFile.getFileData();
		
		// Pass to request/session
		DegreeForm dform = (DegreeForm)request.getSession().getAttribute("dform");
		
		// file name and size to session
		request.getSession().setAttribute("filename", filename);
		request.getSession().setAttribute("filesize", filesize);
		
		// save transcript, name, size into degreeform 
		dform.setFilename(filename);
		dform.setFilesize(String.valueOf(filesize));
		dform.setTranscript(myFile);
		
		return mapping.findForward("success");
	}
}